<template>
  <view class="container">	
	<view class="input" style="width:80%">
		<uni-easyinput v-model="numbers"   placeholder="请输入号码" type="number"  style="width: 70%;"/>
	</view>
	 <uni-row>
	 	<uni-col :span="24">
	 		<view >
	 			<textarea id="resultTextarea" v-model="content"  disabled class="ctextarea"></textarea>
				<text >{{count}}注</text>
			</view>
	 	</uni-col>
	 </uni-row>
	 <uni-row style="top: 25px"  :gutter="50">
	 	<uni-col :span="8" >
	 		<button class="button" @click="optionResult">
	 			<text class="button-text">计算</text>
	 		</button>
	 	</uni-col>
	 	<uni-col :span="8"  >
	 		<button class="button" @click="clear">
	 			<text class="button-text">清空</text>
	 		</button>
	 	</uni-col>
	 	<uni-col :span="8"  >
	 		<button class="button" @click="saveRecord">
	 			<text class="button-text">提交</text>
	 		</button>
	 	</uni-col>
	 </uni-row>
  </view>
</template>

<script>
export default {
  data() {
    return {
		type:'plw',
		num:[],
		content:'',
		count:0,
		numbers:'',
		arr : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
    };
  }, 
  onLoad(query) {
  	if (query.args) {
  		this.args = JSON.parse(query.args);
  	}
  },
  methods: {
	 optionResult(){
		if(this.numbers.length==0){
			uni.showToast({
				title: '号码必须填写！'
			});
		 return 
		}
		let numberArray = this.numbers.split("")
		numberArray = numberArray.map(Number);
		let result = []
		this.optionType = 48
		this.optionName = "胆码"
		let a1  = this.generateCombinationsForOneArray(this.arr, this.arr, this.arr);
		result = a1.filter(item => {
		let itemCalculateDif = this.sumOfDigitsAbsoluteValues(item)
			return this.hasIntersection(itemCalculateDif,numberArray)
		})
		 
		// 排序数组
		result.sort((a, b) => a - b);
		// 去重
		result = [...new Set(result)];
		this.content = result.join(" ")
		this.count = result.length
		//this.saveRecord(myres)
	 },
	 saveRecord(){
	 		let numberArray = this.numbers.split("");
	 		if(numberArray.length==0){
	 			uni.showToast({
	 				title: '号码必须填写！'
	 			});
	 			return 
	 		}
	 		if(this.content.length==0){
	 			uni.showToast({
	 				title: '计算结果为空！'
	 			});
	 			return 
	 		}
	 		numberArray = numberArray.map(Number);
	 		let data = {};
	 		const str = JSON.stringify(numberArray)
	 		const deviceId = uni.getStorageSync("phone");
	 		data.type = this.args.type; // 彩票类型
	 		//data.optionType = this.optionType //按钮的类型编号 比如 组排 的 0 百位 1
	 		data.optionName = "任意两码和" //this.optionName //按钮的类型名称  比如 组排 百位
	 		data.deviceId = deviceId; // 设备唯一编号 可以是手机号
	 		data.record = str;
	 		data.projectName = this.args.projectName
	 		data.result = JSON.stringify(this.content.split(" "))
	 		// 0 代表从走势图查看的 并且需要在个人记录中查看
	 		data.flag=2
	 		this.$myRequest({
	 			method: 'post',
	 			url: '/saveRecord',
	 			data: data
	 		}).then((res) => {
	 				if(res.flag){
	 					setTimeout(function() {
	 						 uni.navigateBack();
	 					},200)
	 				}
	 				
	 		});
	 } ,
	 generateCombinationsForOneArray(arr1, arr2, arr3) {
	   return arr1.flatMap(i =>   
	     arr2.flatMap(j =>   
	       arr3.map(k => `${i}${j}${k}`)  
	     )  
	   );  
	 },
	 clear(){
		 this.num = []
		 this.numbers =""
		 this.content = ""
		 this.count =0
	 },
	 sumOfDigitsAbsoluteValues(number) { //三位数和的绝对值
	   // 将3位数转换为字符串
	   const str = number.toString();
	   // 计算每对位数之间的差的绝对值
	   const sum1 = Math.abs(Number(str.charAt(0)) + Number(str.charAt(1)))%10;
	   const sum2 = Math.abs(Number(str.charAt(1)) + Number(str.charAt(2)))%10;
	   const sum3 = Math.abs(Number(str.charAt(0)) + Number(str.charAt(2)))%10;
	   // 返回结果数组
	   return [sum1, sum2, sum3];
	 },
	 hasIntersection(arr1, arr2) { // 判断是否有交集
	   return arr1.filter(item => arr2.includes(item)).length > 0;
	 },
	 copy(){
		  const textToCopy = this.result;
		  // 复制功能
		  uni.setClipboardData({
		  	data: textToCopy, // 要复制到剪贴板的文本
		  	success: () => {
		  		// 成功回调函数
		  		uni.showToast({
		  			title: '复制成功',
		  			icon: 'success'
		  		});
		  	},
		  	fail: () => {
		  		// 失败回调函数
		  		uni.showToast({
		  			title: '复制失败',
		  			icon: 'none'
		  		});
		  	}
		  });
	  }
	}
  }
</script>

<style>
	
.ctextarea{
	height:200px;
	background-color: white;
	border-radius: 10px;
	width: 100%;            /* 设置宽度 */
	padding: 10px;        /* 内边距 */
	box-sizing: border-box; /* 包括内边距和边框 */
}
	
.container {
  height: 100vh;
  padding: 10px;  
  background-color: #f6eee8;  
  border-radius: 10px;  
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1); 
}

 .input{
	   display: flex; /* 使用 Flexbox 布局 */
	   align-items: center; /* 垂直居中对齐 */
	   margin-bottom: 20px;
	   margin-left: 5%;
	   margin-top:10px;
 }
 .button {
 	display: flex;
 	justify-content: center; /* 水平居中 */
 	align-items: center; /* 垂直居中 */
 }
 .button1 {
 	display: flex;
 	justify-content: center; /* 水平居中 */
 	align-items: center; /* 垂直居中 */
 	width: 80px; /* 按钮宽度 */
 	height: 30px; /* 按钮高度 */
 	border: 1px solid #ccc; /* 边框样式 */
 	border-radius: 30px; /* 圆角 */
 }
 .button-text {
 	font-size: 10px; /* 字体大小 */
 	color: #333; /* 文字颜色 */
 	text-align: center; /* 文字水平居中 */
 	color: black; /* 初始字体颜色 */
 }
 
</style>
